#include <winhack.h>
#include <elasysapi.h>
#include "hacker.h"
#include "mouse.h"
#include "keyboard.h"
#include "video.h"
#include "notifiercenter.h"
#include <aura.h>
#include "mxml.h"
#include "simulator.h"



EXTERN_C  char *getenv(const char *varname);

bool  g_isSimulator=false;
BYTE* g_sharedMemoryAddress = NULL;
Aura_Handle g_sharedmemory=NULL;

bool IsSimulator()
{
    return g_isSimulator;
}

void GetWndInfoFromConfigXml(
        /* [out] */ Int32 * pPosX,    //100
        /* [out] */ Int32 * pPosY,    //100
        /* [out] */ Int32 * pWidth,   //300
        /* [out] */ Int32 * pHeight   //300
        )
{
    wchar_t imageName[64];
    wchar_t cmdlineBuf[_MAX_PATH];
    g_isSimulator = false;
#if _DEBUG
    ECode ec =
#endif
    AuraProcess_GetStartInfo(imageName, cmdlineBuf);
#if _DEBUG
    assert(SUCCEEDED(ec));
#endif
    wchar_t* argv[32]={NULL};
    UINT32 argc = 0;
    wchar_t* p = cmdlineBuf;
    wchar_t* endpoint = cmdlineBuf+wcslen(cmdlineBuf);
    while(p < endpoint)
    {
        if(argc > sizeof(argv)) return ;
        if(L'\"' == *p)
        {
            p++;
            argv[argc] = p;
            while(p < endpoint && *p!=L'\"') p++;
            if(L'\"' == *p)
            {
                *p=0;
                p++;
            }
        }
        else {
            argv[argc] = p;
            while(p < endpoint && L' ' != *p) p++;
            if(L' ' == *p)
            {
                *p=0;
                p++;
            }
        }
        argc++;
        while(p < endpoint && L' ' == *p) p++;
    }

    bool hasGetScreenInfo =false;
    if(argc>=6){ //superexe a.dll 0 0 110 200
        hasGetScreenInfo =true;
        int temp[4];
        //if failed ,we should keep the default value
        temp[0] = _wtoi(argv[argc-4]);
        temp[1] = _wtoi(argv[argc-3]);
        temp[2] = _wtoi(argv[argc-2]);
        temp[3] = _wtoi(argv[argc-1]);

        if(temp[0]>=0&&temp[1]>=0&&temp[2]>0&&temp[3]>0)
        {
            hasGetScreenInfo =true;
            *pPosX = temp[0];
            *pPosY = temp[1];
            *pWidth = temp[2];
            *pHeight =temp[3];
        }
    }

    if(0 == argc) return ;
    
    UINT32 pathPos;
    for(pathPos=0;pathPos<argc;pathPos++){
        if (argv[pathPos][0] != L'-' && argv[pathPos][0] != L'/') {
            break;
        }
    }


    wchar_t path[_MAX_PATH];
    if(NULL== *(argv[pathPos]))
        return ;

    wcscpy(path, argv[pathPos]);
    wcscat(path, L"\\config.xml");

    FILE* fp =  _wfopen(path,L"r");
    if (NULL == fp) {
        return;
    }

    mxml_node_t* pXml = NULL;
    mxml_node_t* pXmlTree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK);

    if (NULL == pXmlTree) {
        fclose(fp);
        return;
    }

    if(!hasGetScreenInfo){
        pXml = mxmlFindElement(pXmlTree, pXmlTree, "widget",
        NULL, NULL, MXML_DESCEND_FIRST);
        if (NULL == pXml) {
            fclose(fp);
            return;
        }

        const char* pXmlLeft = mxmlElementGetAttr(pXml, "left");
        if (NULL == pXmlLeft) {
            return ;
        }
        else {
            *pPosX = atoi(pXmlLeft);
        }

        const char* pXmlTop = mxmlElementGetAttr(pXml, "top");
        if (NULL == pXmlTop) {
            return ;
        }
        else {
            *pPosY = atoi(pXmlTop);
        }

        const char* pXmlWidth = mxmlElementGetAttr(pXml, "width");
        if (NULL == pXmlWidth) {
            return ;
        }
        else {
            *pWidth = atoi(pXmlWidth);
        }

        const char* pXmlHeight = mxmlElementGetAttr(pXml, "height");
        if (NULL == pXmlHeight) {
            return ;
        }
        else {
            *pHeight = atoi(pXmlHeight);
        }
    }

    pXml = mxmlFindElement(pXml, pXml, "simulator",NULL, NULL, MXML_DESCEND_FIRST);
    if(pXml)
    {
        SimulatorInfo simulatorInfo;
        memset(&simulatorInfo,0,sizeof(SimulatorInfo));
        simulatorInfo.MobileWidth=*pWidth;
        simulatorInfo.MobileHeight=*pHeight;
        simulatorInfo.MobilePosX=*pPosX;
        simulatorInfo.MobilePosY=*pPosY;

        const char* pProduct = mxmlElementGetAttr(pXml,"product");
        if (NULL == pProduct) {
            goto error;
        }
        else {
            strcpy(simulatorInfo.product,pProduct);
        }

        const char* pLCDLeft = mxmlElementGetAttr(pXml,"left");
        if (NULL == pLCDLeft) {
            goto error;
        }
        else {
            simulatorInfo.lcdOffsetX = atoi(pLCDLeft);
        }

        const char* pLCDTop = mxmlElementGetAttr(pXml,"top");
        if (NULL == pLCDTop) {
            goto error;
        }
        else {
            simulatorInfo.lcdOffsetY = atoi(pLCDTop);
        }

        const char* pLCDWidth = mxmlElementGetAttr(pXml,"width");
        if (NULL == pLCDWidth) {
            goto error;
        }
        else {
            simulatorInfo.lcdWidth = atoi(pLCDWidth);
        }

        const char* pLCDHeight = mxmlElementGetAttr(pXml,"height");
        if (NULL == pLCDHeight) {
            goto error;
        }
        else {
            simulatorInfo.lcdHeight = atoi(pLCDHeight);
        }

        simulatorInfo.showLCDOnly=false;
        const char* pShowLCDOnly = mxmlElementGetAttr(pXml,"showLCDOnly");
        if (pShowLCDOnly&&0==_strnicmp(pShowLCDOnly,"true", strlen("true"))) {
            simulatorInfo.showLCDOnly=true;
        }

        const char* pLCDColor = mxmlElementGetAttr(pXml,"colorformat");
        if (NULL == pLCDColor) {
            goto error;
        }
        else {
            strcpy(simulatorInfo.colorFormatStr,pLCDColor);
            if(0==_strnicmp(pLCDColor, "RGB565", strlen("RGB565"))){
                simulatorInfo.pixleByte=2;
                simulatorInfo.colorFormat=COLOR_RGB565;
            }
            else if(0==_strnicmp(pLCDColor, "RGBT555",strlen("RGBT555"))){
                simulatorInfo.pixleByte=2;
                simulatorInfo.colorFormat=COLOR_RGBT555;
            }
            else if(0==_strnicmp(pLCDColor, "RGBT666",strlen("RGBT666"))){
                simulatorInfo.pixleByte=4;
                simulatorInfo.colorFormat=COLOR_RGBT666;
            }
            else
                goto error;
        }
        char* xdk_target_product=getenv("XDK_TARGET_PRODUCT");
        if(xdk_target_product)
            strcpy(simulatorInfo.xdk_target_product,xdk_target_product);
        simulatorInfo.simulatoHostStarted=false;
        Int32 memSize=sizeof(SimulatorInfo)+simulatorInfo.lcdWidth*simulatorInfo.lcdHeight*simulatorInfo.pixleByte;
        ECode ec=AuraSharedMemory_Create((Aura_PWChar)SIMULATOR_SHAREMEM_NAME,memSize,AURA_TRUE,AURA_FALSE,&g_sharedmemory);
        assert(g_sharedmemory);
        ec=AuraSharedMemory_Attach(g_sharedmemory, Aura_MemoryProtection_RW, (Aura_Address *)&g_sharedMemoryAddress);
        assert(g_sharedMemoryAddress);
        memcpy(g_sharedMemoryAddress,&simulatorInfo,sizeof(SimulatorInfo));
        g_isSimulator = true;
    }
error:
    if(pXmlTree) mxmlRelease(pXmlTree);
    fclose(fp);
}
